package com.hn658.user.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.logging.enums.LogCategory;
import com.hn658.user.common.entity.DepartmentEO;
import com.hn658.user.common.entity.EmployeeEO;
import com.hn658.user.core.dao.db.IDepartmentDBDAO;
import com.hn658.user.core.dao.db.IEmployeeDBDAO;
import com.hn658.user.core.service.IDepartmentService;
import com.hn658.user.core.service.ISync;
import com.hn658.user.sync.department.Department;
import com.hn658.user.sync.department.DepartmentHelper;
import com.hn658.user.sync.user.Employee;
import com.hn658.user.sync.user.EmployeeHelper;

@Transactional(propagation=Propagation.SUPPORTS)
@Service
public class Sync extends AbstractBusinessObject implements ISync{
	@Autowired
	private IDepartmentDBDAO departmentDBDAO;
	@Autowired
	private IEmployeeDBDAO employeeDBDAO;
	@Autowired
	private IDepartmentService departmentService;
	@Override
	public void departmentSync() {
		List<Department> listDepartments = DepartmentHelper.listDepartments();
		for(Department department:listDepartments){
			try{
				DepartmentEO departmentEO=new DepartmentEO();
				departmentEO.setId(department.getId());
				departmentEO.setDeptName(department.getName());
				departmentEO.setParentDeptId(department.getParentid());
				Department departmentsDetail = DepartmentHelper.departmentsDetail(department.getId());
				departmentEO.setDisplayOrder(Integer.parseInt(departmentsDetail.getOrder()));
				//用ID去查询数据库，如果数据库有，那么更新，如果数据库没有，那么插入
				DepartmentEO getDepartment = departmentService.getDepartment(department.getId());
				if(getDepartment!=null){
					departmentService.modifyDepartment(departmentEO);
				}else{
					//插入ROOT节点
					if(department.getParentid()==null){
						departmentEO.setIsDeleted(false);
						departmentEO.setCreateTime(new Date());
						departmentEO.setLastUpdateTime(new Date());
						departmentDBDAO.insert(departmentEO);
					}else{
						departmentEO.setIsDeleted(false);
						departmentEO.setCreateTime(new Date());
						departmentEO.setLastUpdateTime(new Date());
						departmentDBDAO.insert(departmentEO);
					}	
				}
			}catch(Exception e){
				logger.error(LogCategory.INTERFACE, e.getMessage(),e);
			}
		}
	}
	@Override
	public void employeeSync() {
		List<Department> listDepartments = DepartmentHelper.listDepartments();
		for(Department department:listDepartments){
			if(department.getParentid()!=null){
				List<Employee> departmentUser = EmployeeHelper.getUserDetails(department.getId());
				for(Employee employee:departmentUser){
					EmployeeEO employeeEO=new EmployeeEO();
					employeeEO.setUid(employee.getUserid());
					employeeEO.setEmpName(employee.getName());
					employeeEO.setDeptId(department.getId());
					employeeEO.setCreateTime(new Date());
					employeeEO.setEmail(employee.getEmail());
					employeeEO.setEmpPosition(employee.getPosition());
					employeeEO.setJobnumber(employee.getJobnumber());
					employeeEO.setLastUpdateTime(new Date());
					employeeEO.setMobile(employee.getMobile());
					
					EmployeeEO employeeEOById = employeeDBDAO.selectByIdToEmp(employee.getUserid());
					if(employeeEOById!=null){
						employeeDBDAO.updateEmployee(employeeEO);
					}else{
						employeeDBDAO.addEmployee(employeeEO);
					}
				}				
			}
		}
	}
	
}
